package com.cyan.app.workflow.activity.UserCenter;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.ClickUtil;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by DXW on 2016/2/27.
 */
public class MotifyPwdActivity extends BaseFragmentActivity {
    public static final String TAG = "MotifyPwdActivity";
    private Button mButtonSubmit;
    private EditText mPwd;
    private EditText mNewPwd;
    private EditText mNewPwd1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_modifypwd);
        titleBar();

        mPwd = (EditText)findViewById(R.id.user_modify_pwd);
        mNewPwd = (EditText)findViewById(R.id.user_modify_newpwd);
        mNewPwd1 = (EditText)findViewById(R.id.user_modify_newpwd1);
        mButtonSubmit = (Button)findViewById(R.id.user_modify_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isFastClick(10)) {
                    //10秒内不能重复操作
                    return;
                }
                if (mPwd.getText().toString().isEmpty()) {
                    AppUtils.showTip(MotifyPwdActivity.this, "当前密码不能为空");
                } else if (mNewPwd.getText().toString().isEmpty()) {
                    AppUtils.showTip(MotifyPwdActivity.this, "新密码不能为空");
                } else if (mNewPwd1.getText().toString().isEmpty()) {
                    AppUtils.showTip(MotifyPwdActivity.this, "确认密码不能为空");
                } else if (!mNewPwd1.getText().toString().equals(mNewPwd.getText().toString())) {
                    AppUtils.showTip(MotifyPwdActivity.this, "新密码和确认密码不一致");
                }  else {
                    execApi();
                }
            }
        });


    }
    //执行流转
    private void execApi(){
        String result = null;
        try {
            Map<String, String> params = new HashMap<String, String>();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userid", AppData.getInstance().getUserId());
            jsonObject.put("pwd", mPwd.getText());
            jsonObject.put("newpwd", mNewPwd.getText());
            params.put("action", "102");
            params.put("params", jsonObject.toString());
            VolleyUtil.sendStringRequestByGet(MotifyPwdActivity.this, HttpConfig.HTTP_MAIN_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
                @Override
                public void onResponse(String result) {
                    super.onResponse(result);
                    if (result == null) {
                        return;
                    }
                    BaseBean resultBean = new Gson().fromJson(result, BaseBean.class);

                    if (resultBean.getCode() == 1) {
                        AppUtils.showTip(MotifyPwdActivity.this, "修改成功");
                        finish();
                    } else {
                        AppUtils.showTip(MotifyPwdActivity.this, resultBean.getMsg());
                    }
                }

                @Override
                public void onCodeError(int code, String msg) {
                    AppUtils.showTip(MotifyPwdActivity.this, msg);
                }

                @Override
                public void onVolleyError(VolleyError volleyError) {
                    AppUtils.showTip(MotifyPwdActivity.this, R.string.network_error);
                }

                @Override
                public void onException(Exception e) {
                    AppUtils.showTip(MotifyPwdActivity.this, R.string.network_error);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            AppUtils.showTip(MotifyPwdActivity.this, "执行失败，请联系IT人员");
        }
    }
    private void titleBar() {
        TextView mTitleName = (TextView) findViewById(R.id.top_title_info);
        mTitleName.setText("修改密码");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }

}
